package com.htrq.controller;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.htrq.entity.TResource;
import com.htrq.entity.User;
import com.htrq.service.LoginService;
import com.htrq.util.Md5cUtil;

@Controller
public class LoginController {
	@Resource
	private LoginService loginService;

	@RequestMapping("syslogin")
	public String login(User user, HttpServletRequest request) throws NoSuchAlgorithmException,
			UnsupportedEncodingException {
		User reluser = loginService.getUser(user.getUname());
		if (reluser == null) {
			return "login";
		} else {
			boolean flag = reluser.getUpwd().equals(Md5cUtil.encoderByMd5(user.getUpwd()));
			if (user.getUpwd() != null && flag) {
				// 密码正确
				HttpSession session = request.getSession();
				session.setAttribute("user", reluser);
				List<TResource> rights = loginService.getRightByUser(reluser);
				session.setAttribute("right", rights);
				return "jsp/sys/index";
			}
			return "login";
		}
	}
}
